package com.jty.module_main.ui.activity

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.lib_base.config.AppConstants
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.jty.lib_base.adapter.ViewPagerFmAdapter
import com.jty.lib_base.base.AppManager
import com.jty.lib_base.base.BaseActivity
import com.jty.lib_base.event.LiveBusCenter
import com.jty.lib_base.route.RouteCenter
import com.jty.module_main.BR
import com.jty.module_main.R
import com.jty.module_main.databinding.MainActivityMainBinding
import com.jty.module_main.viewmodel.MainViewModel
import me.yokeyword.fragmentation.SupportFragment

@Route(path = AppConstants.Router.Main.A_MAIN)
class MainActivity : BaseActivity<MainActivityMainBinding, MainViewModel>() {


    private var touchTime: Long = 0L

    private val colors = listOf(R.color.green_9c,R.color.white_cc)
    private val mipmaps = listOf(R.drawable.ic_jlu_on,R.drawable.ic_me_on)
    private val mipmaphls = listOf(R.drawable.ic_jlu_off,R.drawable.ic_me_off)
    private val tabTitles = listOf("笔记","我的")
    override fun initContentView(): Int {
        return R.layout.main_activity_main
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }


    override fun useBaseLayout(): Boolean {
        return false
    }

    override fun initViewObservable() {
        viewModel.uc.tabChangeLiveEvent.observe(this) {
            binding.viewPage2.setCurrentItem(it, false)
        }

    }

    override fun initData() {
        setSwipeBackEnable(false)
        initViewPager()
        initBottomBar()
    }

    private fun initViewPager() {
        binding.viewPage2.isUserInputEnabled = false
        val homeFragment = RouteCenter.navigate(AppConstants.Router.Main.F_HOME) as SupportFragment
        val userFragment = RouteCenter.navigate(AppConstants.Router.User.F_USER) as SupportFragment
        val fragments = arrayListOf(homeFragment,userFragment)
        binding.viewPage2.apply {
            adapter = ViewPagerFmAdapter(supportFragmentManager,lifecycle,fragments)
            offscreenPageLimit = fragments.size
        }
    }

    private fun initBottomBar() {

        TabLayoutMediator(binding.tlBottom, binding.viewPage2) { tab, position ->
            tab.text = tabTitles[position]
            tab.customView = getTabView(position)
            if (position == 0) setTextColor(tab.customView,position,true) else setTextColor(tab.customView,position,false)
        }.attach()

        binding.tlBottom.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                setTextColor(tab?.customView, tab!!.position, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                setTextColor(tab?.customView, tab!!.position, false)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

    }


    private fun getTabView(curPos: Int): View? {
        val view: View = LayoutInflater.from(this).inflate(R.layout.main_home_tab, null)
        val igv = view.findViewById<View>(R.id.tab_item_iv) as ImageView
        val tv = view.findViewById<View>(R.id.tab_item_tv) as TextView
        tv.text = tabTitles[curPos]
        igv.setImageResource(mipmaps[curPos])
        setTextColor(view, curPos, false)
        return view
    }

    fun setTextColor(view: View?, position: Int, select: Boolean) {
        if (null != view && view is LinearLayout) {
            val tv = view.findViewById<View>(R.id.tab_item_tv) as TextView
            val igv = view.findViewById<View>(R.id.tab_item_iv) as ImageView
            tv.setTextColor(ContextCompat.getColor(this,
                if (select) colors[0] else colors[1]))
            igv.setImageResource(if (select) mipmaps[position] else mipmaphls[position])
        }
    }

    override fun onBackPressedSupport() {
        if (System.currentTimeMillis() - touchTime < 2000L) {
            LiveBusCenter.postLogoutEvent(3)
            AppManager.instance.appExit()
        } else {
            touchTime = System.currentTimeMillis()
            showNormalToast(getString(R.string.main_press_again))
        }
    }
}